/*
 * @Description:
 * @Version: 2.0
 * @Autor: wangmiao
 * @Date: 2022-08-17 15:09:51
 * @LastEditors: wangmiao
 * @LastEditTime: 2022-08-20 18:04:49
 */
// 生成guid
export const guid = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    var r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};

// 数组转树结构
export const ReorganizationData = (data: any, Id: string, ParentId: string) => {
  // 将数据存储为 以 id 为 KEY 的 map 索引数据列
  let map: any = {};
  data.forEach(function (item: any) {
    map[item[Id]] = item;
  });
  let val: any = [];
  data.forEach(function (item: any) {
    // 以当前遍历项，的pid,去map对象中找到索引的id
    let parent = map[item[ParentId]];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
      val.push(item);
    }
  });
  return val;
};

// 时间转换格式

export function dateFormat(time: any, type: string) {
  let formatTime;
  let date = time || new Date();
  console.log(date);
  // if(time === 0) {
  //   date = new Date()
  // } else {
  //   date = new Date(time)
  // }
  let Year = date.getFullYear();
  let month = date.getMonth() + 1;
  let Month = month >= 10 ? month : "0" + month;
  let day = date.getDate();
  let Day = day >= 10 ? day : "0" + day;
  let Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let Minute =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let Second =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  if (type === "yyyy-MM-dd") {
    formatTime = Year + "-" + month + "-" + day;
    return formatTime;
  } else if (type === "yyyy-MM-dd hh:mm:ss") {
    formatTime =
      Year + "-" + month + "-" + day + " " + Hour + ":" + Minute + ":" + Second;
    return formatTime;
  } else if (type === "hh:mm:ss") {
    formatTime = Hour + ":" + Minute + ":" + Second;
    return formatTime;
  } else {
    return "error type!";
  }
}

// 获取地址栏信息

export const getParams = (name: any) => {
  let url = window.location.href;
  let str = url.indexOf("?");
  let num = url.slice(str + 1);
  let a = num.split("&");
  var list: any = {};
  for (var key in a) {
    list[key] = a[key];
    let b = list[key].indexOf("=");
    let c = list[key].substring(0, b);
    if (c == name) {
      let d = list[key].substring(b + 1);
      return d;
    }
  }
};
